package idxSonType;

import dbTools.DBTools;
import idxType.IdxClassifyBean;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

/**
 * Servlet implementation class IdxAddSonServlet
 */
@WebServlet("/IdxAddSonServlet")
public class IdxAddSonServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public IdxAddSonServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.setContentType("text/html;charset=GBK");
		request.setCharacterEncoding("GBK");
		response.setCharacterEncoding("GBK");
		String idxTypeId=request.getParameter("id");
		System.out.println(idxTypeId);
		String sql1="SELECT * FROM idxTypeInfo sti LEFT JOIN idxSonInfo ssi ON sti.idxTypeId=ssi.idxId WHERE sti.idxTypeId="+idxTypeId+";";
		DBTools db=new DBTools();
		db.open();
		IdxSonClassifyBean sc=null;
		ArrayList al1=new ArrayList();
		ResultSet rs1=db.select(sql1);
		try {
			while (rs1.next()) {
				sc=new IdxSonClassifyBean(rs1.getInt(1),rs1.getString(2),rs1.getInt(3),rs1.getString(4),rs1.getInt(5));
				al1.add(sc);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		request.setAttribute("sc", sc);
		String sql2="SELECT * FROM idxTypeInfo WHERE idxTypeId="+idxTypeId+";";
		ResultSet rs2=db.select(sql2);
		ArrayList al2=new ArrayList();
		IdxClassifyBean sc2=null;
		try {
			while (rs2.next()) {
				sc2=new IdxClassifyBean(rs2.getInt(1),rs2.getString(2));
				al2.add(sc2);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		request.setAttribute("al", al2);
		db.close();
		request.getRequestDispatcher("/addidxSonClassify.jsp").forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.setContentType("text/html;charset=GBK");
		request.setCharacterEncoding("GBK");
		response.setCharacterEncoding("GBK");
		int i=1;
		int i2=0;
		String idxType=request.getParameter("idxType");
		String idxTypeId=request.getParameter("idxId");
		String sql="INSERT INTO idxSonInfo(idxSonType,idxId) VALUES ('"+idxType+"',"+idxTypeId+");";
		String sql1="SELECT * FROM idxTypeInfo sti LEFT JOIN idxSonInfo ssi ON sti.idxTypeId=ssi.idxId WHERE sti.idxTypeId="+idxTypeId+";";
		DBTools db=new DBTools();
		db.open();
		db.update(sql);
		IdxSonClassifyBean sc=null;
		ArrayList al1=new ArrayList();
		ResultSet rs1=db.select(sql1);
		try {
			while (rs1.next()) {
				sc=new IdxSonClassifyBean(rs1.getInt(1),rs1.getString(2),rs1.getInt(3),rs1.getString(4),rs1.getInt(5));
				al1.add(sc);
			}
			if(al1.size()%5!=0){
				i2=al1.size()/5+1;
				if(i<1){
					i=al1.size()/5+1;
				}
				if(i>al1.size()/5+1){
					i=1;
				}	
			}else{
				i2=al1.size()/5;
				if(i<1){
					i=al1.size()/5;
				}
				if(i>al1.size()/5){
					i=1;
				}	
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		request.setAttribute("sc", sc);
		String sql2="SELECT * FROM idxTypeInfo sti LEFT JOIN idxSonInfo ssi ON sti.idxTypeId=ssi.idxId  WHERE sti.idxTypeId="+idxTypeId+" ORDER BY ssi.idxSonId DESC limit "+(i-1)*5+","+"5;";
		ResultSet rs2=db.select(sql2);
		ArrayList al2=new ArrayList();
		try {
			while (rs2.next()) {
				sc=new IdxSonClassifyBean(rs2.getInt(1),rs2.getString(2),rs2.getInt(3),rs2.getString(4),rs2.getInt(5));
				al2.add(sc);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		request.setAttribute("al", al2);
		request.setAttribute("i", i);
		request.setAttribute("i2", i2);
		db.close();
		request.getRequestDispatcher("/idxSonClassify.jsp").forward(request, response);
	}

}
